package com.cy.src.sqlite.upgrade;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by CY on 2016/10/24.
 */

public class MergeOpenHelper extends SQLiteOpenHelper {

    public MergeOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public MergeOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createAllTables(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {

            new UpgradeHelper().upgrade(db, this);
        }
    }

    public void insert(SQLiteDatabase db) {
        String sql = "INSERT INTO user (name,age,sex,profession)VALUES('cy','24','男','IT')";
        db.execSQL(sql);
    }

    private void createAllTables(SQLiteDatabase db) {

        String sql = "CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY,name TEXT,age INTEGER,sex TEXT,profession TEXT)";
        db.execSQL(sql);

    }

}
